﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace DAL
{
    public class UserData
    {
        //用户对于相关数据表的操作类
        //用户注册逻辑层方法
        public int CreatUser(string name, string password, string gender, string email)
        {
            try
            {               
                Users usmodel = new Users();
                usmodel.user_id = 0;
                usmodel.name = name;
                usmodel.password = password;
                usmodel.gender = gender;                
                usmodel.email = email;
                usmodel.authority = 0;      //默认为普通用户；
                ////创建上下文对象，用来操作数据库
                ProjectDBEntities pde = new ProjectDBEntities();
                //检测用户是否存在
                var u = from c in pde.Users
                        where c.name == name
                        select c;
                if(u.Count() == 0)
                {
                //添加一个用户的方法
                pde.AddToUsers(usmodel);
                //写入数据库
                return pde.SaveChanges();
                }
                else 
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        //用户登录验证方法
        public int Login(string Name, string Password)
        {
            //创建上下文对象，用来操作数据库
            ProjectDBEntities pde = new ProjectDBEntities();
            //用linq语句查询出对应输入的用户名的密码
            var u = from c in pde.Users
                    where (c.name == Name && c.password == Password)
                    select c;
            //查询到的数据为空时，表示用户输入的用户名或密码错误，验证失败，返回0；
            if (u.Count() == 0)
            {
                return 0;
            }
             //验证正确，判断用户的权限，普通会员返回1，管理员返回2；
            else
            {
                foreach (Users n in u)
                {
                    if (n.authority == 0)
                    {
                        return 1;
                    }
                    else if (n.authority == 1)
                    {
                        return 2;
                    }
                }
                return 0;
            }
        }
        //查询用户基本信息方法 返回类型为Users类的对象
        public Users MessageData(string Name)
        {
            //创建上下文对象，用来操作数据库
            ProjectDBEntities pde = new ProjectDBEntities();
            //用linq语句查询出对应输入的用户名的对象
            var u = from c in pde.Users
                    where c.name == Name
                    select c;
            foreach (Users n in u)
            {
                return n;
            }
            return null;
        }
        //用户更改信息数据层处理函数
        public int ChangeData(string Name, string Password, string Gender, string Age, string Email)
        {
            //创建上下文对象，用来操作数据库
            ProjectDBEntities pde = new ProjectDBEntities();
            //用linq语句查询出对应输入的用户名的对象
            var u = from c in pde.Users
                    where c.name == Name
                    select c;
            foreach (Users n in u)
            {
                if (Password != "")
                {
                    n.password = Password;
                }
                if (Age != "")
                {
                    n.age = Convert.ToInt32(Age);
                }
                if (Email != "")
                {
                    n.email = Email;
                }
                if (Gender != "")
                {
                    n.gender = Gender;
                }             
                
               
            }
            return pde.SaveChanges();
        }
        public int DelData(int U_id)
        {
            Users model = new Users();
            model.user_id = U_id;
            //创建上下文对象，用来操作数据库
            ProjectDBEntities pde = new ProjectDBEntities();
            pde.Users.Attach(model);           
            pde.ObjectStateManager.ChangeObjectState(model, System.Data.EntityState.Deleted);
            return pde.SaveChanges();
           // pde.Response.Write("删除成功");
            
        }
    }
}

